package com.hellobike.haxing.modules.wms.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hellobike.haxing.modules.wms.entity.StockBatchAtt;
import com.hellobike.haxing.modules.wms.mapper.StockBatchAttMapper;
import com.hellobike.haxing.modules.wms.service.IStockBatchAttService;
import com.hellobike.haxing.modules.wms.utils.WmsCodeGeneratorUtil;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * @Description: t_wms_stock_batch_att
 * @Author: haxing-boot
 * @Date:   2020-02-18
 * @Version: V1.0
 */
@Service
public class StockBatchAttServiceImpl extends ServiceImpl<StockBatchAttMapper, StockBatchAtt> implements IStockBatchAttService {

    @Override
    public StockBatchAtt getOrCreateBatch(Long skuId, String date, Integer damaged, Long warehouseId) {
        StockBatchAtt param = new StockBatchAtt();
        param.setSkuId(skuId);
        param.setProductionDate(date);
        param.setDamagedFlag(damaged);
        param.setWarehouseId(warehouseId);
        Wrapper<StockBatchAtt> wrapper = new QueryWrapper<>(param);
        StockBatchAtt stockBatchAtt = baseMapper.selectOne(wrapper);
        if(null ==stockBatchAtt){
            param.setBatchNo(WmsCodeGeneratorUtil.createBatchAttCode(warehouseId));
            baseMapper.insert(param);
            return param;
        }else {
            return stockBatchAtt;
        }
    }
}
